package com.masterrabbit.mcslf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.masterrabbit.mcslf.contans.Constants;
import com.masterrabbit.mcslf.contans.ResultCodeEnum;
import com.masterrabbit.mcslf.exception.McslfException;
import com.masterrabbit.mcslf.mapper.FinancialRecordsMapper;
import com.masterrabbit.mcslf.pojo.FinancialRecords;
import com.masterrabbit.mcslf.pojo.Orders;
import com.masterrabbit.mcslf.service.DailyFlowService;
import com.masterrabbit.mcslf.service.FinancialRecordsService;
import com.masterrabbit.mcslf.service.OrdersService;
import com.masterrabbit.mcslf.mapper.OrdersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
* @author hasee
* @description 针对表【orders】的数据库操作Service实现
* @createDate 2025-03-18 17:19:02
*/
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders>
    implements OrdersService{
    @Autowired
    OrdersMapper ordersMapper;

    @Autowired
    DailyFlowService dailyFlowService;

    @Autowired
    ThreadPoolExecutor ThreadPoolExecutor;
    @Autowired
    FinancialRecordsMapper financialRecordsMapper;
    @Override
    public void Verification(String verifiCode, String currentStore)  {
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.lambda().eq(Orders::getOrderNumber,verifiCode);
        Orders orders = ordersMapper.selectOne(ordersQueryWrapper);
        if (orders==null || orders.getStatus() != 1){
            throw new McslfException(ResultCodeEnum.VERIFICED);
        }
        if (!orders.getMerchantId().toString().equals(currentStore)){
            System.out.println(orders.getMerchantId());
            System.out.println(currentStore);
            throw new McslfException(ResultCodeEnum.VERIFICED_MED);
        }
        orders.setStatus(3);
        ordersMapper.updateById(orders);
//        修改流水信息
        FinancialRecords financialRecords = new FinancialRecords();
        financialRecords.setOrderNumber(verifiCode);
        financialRecords.setChangeType(Constants.INCOME);
        financialRecords.setBalanceChange(orders.getPaidAmount());
        financialRecords.setMerchantId(Integer.valueOf(currentStore));
        financialRecords.setCreateTime(new Date());
        financialRecords.setTitle(orders.getOrderNumber());
        financialRecordsMapper.insert(financialRecords);
        CompletableFuture future = CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                System.out.println("异步修改流水");
                dailyFlowService.updataLock(orders,currentStore);
            }
        },ThreadPoolExecutor);
    }

    @Override
    public IPage<Orders> getrecodePage(Map<String, Object> pageparams) {
        IPage<Orders> ordersPage = new Page<>((Integer)pageparams.get("current"),(Integer)pageparams.get("size"));
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
//        ordersQueryWrapper.lambda().eq(Orders::getMerchantId, SecurityUtils.getUserId())
        ordersMapper.selectPage(ordersPage, ordersQueryWrapper );
        return ordersPage;
    }
}




